﻿///<reference path="CompanyModel.ts"/>
///<reference path="AddressModel.ts"/>

module Enferno.Public.Web.Components.Models {

    export class CustomerModel {

        public person: PersonInformationModel;
        public socialSecurityNumber: string;
        public invoiceAddress: AddressModel;
        public deliveryAddresses: Array<AddressModel>;
        public useInvoiceAddressAsDeliveryAddress: boolean;
        public companies: Array<CompanyModel>;

        constructor();
        constructor(serverObject: any);
        constructor(serverObject?: any) {
            if (serverObject != null) {
                this.createFromServerOject(serverObject);
            }
        }

        private createFromServerOject(serverObject: any) {
            this.person = serverObject.person != null ? new PersonInformationModel(serverObject.person) : null;
            this.socialSecurityNumber = serverObject.socialSecurityNumber;
            this.invoiceAddress = serverObject.invoiceAddress != null ? new AddressModel(serverObject.invoiceAddress) : null;
            this.deliveryAddresses = serverObject.deliveryAddresses != null ? Enumerable.from(serverObject.deliveryAddresses).select(deliveryAddress => new AddressModel(deliveryAddress)).toArray() : [];
            this.useInvoiceAddressAsDeliveryAddress = serverObject.useInvoiceAddressAsDeliveryAddress;
            this.companies = serverObject.companies != null ? Enumerable.from(serverObject.companies).select(company => new CompanyModel(company)).toArray() : [];
        }
    }

    export class PersonInformationModel {

        constructor(serverObject?: any) {
            if (serverObject != null) {
                this.email = serverObject.email;
                this.firstName = serverObject.firstName;
                this.lastName = serverObject.lastName;
                this.phone = serverObject.phone;
                this.cellPhone = serverObject.cellPhone;
            }
        }

        public serialize(): any {
            var obj: any = {};
            obj.email = this.email;
            obj.firstName = this.firstName;
            obj.lastName = this.lastName;
            obj.phone = this.phone;
            obj.cellPhone = this.cellPhone;

            return obj;
        }

        public isEmpty(): boolean {
            return this.email == null && this.firstName == null && this.lastName == null && this.phone == null && this.cellPhone == null;
        }

        public email: string;
        public firstName: string;
        public lastName: string;
        public phone: string;
        public cellPhone: string;
    }
} 